<template>
  <fieldset>
    <legend>登入</legend>
    用户名：
    <input type="text" v-model="username" />
    <br />
    密码：
    <input type="text" v-model="password" @keydown.enter="login" />
    <br />
    <button @click="login">点击</button>
  </fieldset>
</template>

<script>
// @默认指向 src
import * as api from "@/api";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    ...mapMutations('users',["SET_USERINFO"]),
    login() {
      let params = {
        username: this.username,
        password: this.password,
      };
      api.loginApi(params).then((res) => {
        console.log(res);
        // 将结果存到 vuex 中
        this.SET_USERINFO(res.userInfo)
        // 跳转到home页面
        if(res.state){
            this.$router.push('/home')
        }else{
            alert(res.msg)
        }
      });
    },
  },
};
</script>